package com.sgb.kjwl.view.ui.activity.manage

import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import androidx.databinding.DataBindingUtil
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.sgb.capital.view.widget.MToast
import com.sgb.kjwl.AppActivity
import com.sgb.kjwl.R
import com.sgb.kjwl.databinding.ActivityAgentStaffCheckDetailBinding
import com.sgb.kjwl.di.manage.DaggerEnterpriseManageComponent
import com.sgb.kjwl.di.manage.EnterpriseManageModule
import com.sgb.kjwl.model.entity.manage.AgentStaffCheckEntity
import com.swgk.core.event.EventConstant
import com.sgb.kjwl.view.event.self.AgentApplyEvent
import com.sgb.link.view.widget.InputDialog
import com.sgb.kjwl.viewmodel.manage.AgentCheckModel
import com.swgk.core.base.di.AppComponent
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import javax.inject.Inject

//员工审核---员工详情
class AgentStaffCheckDetailActivity : AppActivity() {
    @Inject
    lateinit var mViewModel : AgentCheckModel

    private lateinit var mBinding : ActivityAgentStaffCheckDetailBinding

    private var mEntity : AgentStaffCheckEntity ?= null
    private var mRefuseDialog : InputDialog?= null


    companion object{
        const val KEY_ENTITY = "key_entity"

        fun launch(context: Context, entity: AgentStaffCheckEntity){
            val intent = Intent(context, AgentStaffCheckDetailActivity::class.java)
            intent.putExtra(KEY_ENTITY, entity)
            context.startActivity(intent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        EventBus.getDefault().register(this)
    }

    override fun setupActivityComponent(appComponent: AppComponent?) {
        DaggerEnterpriseManageComponent.builder().appComponent(appComponent)
            .enterpriseManageModule(EnterpriseManageModule()).build().inject(this)
    }

    override fun initView() {
        super.initView()
        if (intent != null){
            mEntity = intent.getSerializableExtra(KEY_ENTITY) as AgentStaffCheckEntity?
        }
        if (mEntity == null){
            finish()
            return
        }
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_agent_staff_check_detail)
        mBinding.viewModel = mEntity
        mBinding.include.ivBack.setOnClickListener { finish() }
        mBinding.include.titleView.text = "同意"
        mBinding.tvRefuse.setOnClickListener {
            //拒绝加入
            showRefuseDialog()
        }
        mBinding.tvAgree.setOnClickListener {
            //同意 添加成员页面
            AgentStaffCheckAddActivity.launch(this, mEntity!!)
        }
        Glide.with(this).load(mEntity!!.image).apply(
            RequestOptions()
                .placeholder(R.mipmap.def_photo)
                .fallback(R.mipmap.def_photo)
                .error(R.mipmap.def_photo)
        ).into(mBinding.ivUser)

        mViewModel.rejectSuccess.observe(this, {
            if (!TextUtils.isEmpty(it)) {
                MToast.showToast(this, it)
                EventBus.getDefault().postSticky(
                    AgentApplyEvent.make(EventConstant.EVENT_AGENT_APPLY_REJECT)
                )
                finish()
                mViewModel.rejectSuccess.postValue("")
            }
        })

    }

    private fun showRefuseDialog() {
        if (mRefuseDialog == null){
            mRefuseDialog = InputDialog(this).builder().setNegativeButton("").setPositiveButton("")
                .setEtHint("填写拒绝理由（必填）").setCancelable(false)
                .setDialogCallBack(object : InputDialog.DialogCallBack{
                override fun onRightClick(content: String?, dialog : Dialog) {
                    if (TextUtils.isEmpty(content)){
                         MToast.showToast(this@AgentStaffCheckDetailActivity, "请输入拒绝理由")
                    } else {
                        //请求拒绝接口
                        goToRefuse(content!!)
                        mRefuseDialog!!.reset()
                        dialog.dismiss()
                    }
                }

                override fun onLeftClick() {

                }

            })
        }
        mRefuseDialog!!.show()
    }

    private fun goToRefuse(content: String) {
       mViewModel.rejectAdd(this, content, mEntity!!.id)
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onGetMessage(event : AgentApplyEvent){
        if (event.messageTag == EventConstant.EVENT_AGENT_APPLY_SUCCESS){
            finish()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }
}